class Solution(object):
    def candy(self, ratings):
        """
        :type ratings: List[int]
        :rtype: int
        """
        n = len(ratings)
        left = [1 for i in range(n)]

        for i in range(1, n):
            if ratings[i] > ratings[i-1]:
                left[i] = left[i-1] + 1
    
        total_candys = left[-1]
        right = 1
        for i in range(n-2, -1, -1):
            if ratings[i] > ratings[i+1]:
                right += 1
            else:
                right = 1
            total_candys += max(left[i], right)
        return total_candys
